package com.zyh.dao;
import com.zyh.entity.Comment;
import com.zyh.utils.TransactionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {

    public Comment selectById(Integer commentId) throws SQLException {
        String sql = "SELECT comment_id, user_id, content, created_at, like_count FROM comments WHERE comment_id=?";
        Connection conn = TransactionManager.getCurrentConnection();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, commentId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return new Comment(
                        rs.getInt("comment_id"),
                        rs.getInt("user_id"),
                        rs.getString("content"),
                        rs.getTimestamp("created_at"),
                        null,
                        null,
                        rs.getInt("like_count")
                );
            }
            return null;
        }
    }

    public void add(Comment comment) throws SQLException {
        String sql = "INSERT INTO comments(user_id, content, parent_type, parent_id, like_count, created_at) VALUES(?, ?, ?, ?, 0, NOW())";
        Connection conn = TransactionManager.getCurrentConnection();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, comment.getUserId());
            ps.setString(2, comment.getContent());
            ps.setString(3, comment.getParentType());
            ps.setInt(4, comment.getParentId());
            ps.executeUpdate();
        }
    }

    public void likeById(Integer commentId) throws SQLException {
        String sql = "UPDATE comments SET like_count=like_count+1 WHERE comment_id=?";
        Connection conn = TransactionManager.getCurrentConnection();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, commentId);
            ps.executeUpdate();
        }
    }

    public List<Comment> selectAll() throws SQLException {
        List<Comment> comments = new ArrayList<>();
        String sql = "SELECT comment_id, user_id, content, created_at, like_count FROM comments ORDER BY comment_id";
        Connection conn = TransactionManager.getCurrentConnection();
        try (PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                comments.add(new Comment(
                        rs.getInt("comment_id"),
                        rs.getInt("user_id"),
                        rs.getString("content"),
                        rs.getTimestamp("created_at"),
                        null,
                        null,
                        rs.getInt("like_count")
                ));
            }
        }
        return comments;
    }

    public void updateById(Integer commentId, String newContent) throws SQLException {
        String sql = "UPDATE comments SET content=? WHERE comment_id=?";
        Connection conn = TransactionManager.getCurrentConnection();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, newContent);
            ps.setInt(2, commentId);
            ps.executeUpdate();
        }
    }
}
